﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EasyStep.Business;
using EasyStep.Business.Interfaces;
using EasyStep.Data.Entities;

namespace EasyStep.TestEditor.Controls
{
    public partial class BrowserControl : UserControl
    {
        public ITestDataContext DataContext { get; set; }
        public BrowserControl()
        {
            InitializeComponent();
            this.AutoSize = false;
            this.AutoScroll = true;
            this.HScroll = true;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);


            if (DataContext != null && DataContext.RegisteredBrowsers != null)
            {
                int index = 0;
                foreach (var browser in DataContext.RegisteredBrowsers.Reverse())
                {
                    AddBrowserDetails(browser, index++);
                }
            }

        }
        private void AddBrowserDetails(IWebBrowserProxy browser, int index)
        {            
            var pnl = new Panel() { Name = "pnlBrowser" + index.ToString(), Dock=DockStyle.Left };
            this.Controls.Add(pnl);
            var checkBox = new CheckBox() { Name = browser.Name, Size = new Size(15, 14), UseVisualStyleBackColor = true, Dock = DockStyle.Left, Margin=new Padding(5,0,0,0) };           
            checkBox.CheckedChanged += new EventHandler(checkBox_CheckedChanged);
            pnl.Controls.Add(checkBox);

            var icon = GetBrowserBitmap(browser, null);
            Control image = null;
            if (icon == null)
            {
                image = new Label() { Dock = DockStyle.Left, Text = browser.Name, AutoSize = true, TextAlign = ContentAlignment.MiddleCenter};
                pnl.Controls.Add(image);
                (image as Label).AutoSize = false;
            }
            else
            {
                image = new PictureBox() { Dock = DockStyle.Left, Image = icon, Height = 25, Width = 25 };
                pnl.Controls.Add(image);
            }
            pnl.Width = image.Width + checkBox.Width+25;
            pnl.Height = Math.Max(image.Height, checkBox.Height);            
            
        }

        void checkBox_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox current = sender as CheckBox;
            if (current != null)
            {
                var existing = selectedBrowsers.Where(b => b.Name.Equals(current.Name)).FirstOrDefault();
                if (current.Checked)
                    selectedBrowsers.Add(DataContext.RegisteredBrowsers.Where(b => b.Name.Equals(current.Name)).FirstOrDefault());
                else if (existing != null)
                    selectedBrowsers.Remove(existing);
            }
        }

        private Image GetBrowserBitmap(IWebBrowserProxy browser, Bitmap defaultBitmap)
        {
            if (!System.IO.File.Exists(browser.Icon)) return defaultBitmap;
            var bitmap = Bitmap.FromFile(browser.Icon);
            return bitmap;
        }


        private List<IWebBrowserProxy> selectedBrowsers = new List<IWebBrowserProxy>();
        public List<IWebBrowserProxy> SelectedBrowsers
        {
            get { return selectedBrowsers; }
        }
    }
}